/*
 * Copyright (c) 2016. For Intelligent Group.
 */

package com.intelligent.ispc.export.tolerance;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.core.dto.ToleranceItemDto;
import com.intelligent.ispc.core.dto.ToleranceResultDto;
import com.intelligent.ispc.external.rcall.dto.ToleranceParamDto;
import com.intelligent.ispc.foundation.poi.*;
import com.intelligent.ispc.utils.Constant;
import com.intelligent.ispc.utils.ExportLabelConstant;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Alice on 2016/8/23.
 */
public class TolExportCellFactory implements ExCellFactory {

    private XSSFWorkbook workbook = new XSSFWorkbook();
    private List<ExSheet> sheets = new ArrayList<>();
    private Integer[] headIndex = new Integer[]{0, 0};
    private Integer currentRow = 0;
    private int digNum = 6;

    public void buildTolerance4SingleItem(ToleranceParamDto toleranceParamDto, ToleranceResultDto toleranceResultDto) {

        if (toleranceResultDto == null) {
            throw new ApplicationException(ExceptionMessages.EXCEPTION_PARAMETER_INPUT_INVALID);
        }
        ExSheet exSheet = new ExSheet();
        List<ExCell> cellList = Lists.newArrayList();
        List<ExCell> head = buildHead();
        List<ExCell> content = buildContent(toleranceParamDto);
        List<ExCell> result = buildResult(toleranceParamDto, toleranceResultDto);
        cellList.addAll(head);
        cellList.addAll(content);
        cellList.addAll(result);
        exSheet.setExCells(cellList);

        sheets.add(exSheet);
        exSheet.setName(Constant.EXPORT_DIMENSION_CHAIN);
    }

    public List<ExCell> buildHead() {
        List<ExCell> exCellList = Lists.newArrayList();
        String[] titleLabels = ExportLabelConstant.EXPORT_TOL_SUMMARY_LABELS;

        for (int i = 0; i < titleLabels.length; i++) {
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] , headIndex[1] + i}, titleLabels[i], ExCellType.TEXT, fillTitleStyle() ));
        }
        return exCellList;
    }

    public List<ExCell> buildContent(ToleranceParamDto toleranceParamDto) {
        List<ExCell> exCellList = Lists.newArrayList();
        List<ToleranceItemDto> toleranceItemDtoList = toleranceParamDto.getAnalysisData();
//        String[] lastItem = ExportLabelConstant.LASTITEM;

        for (int i = 0; i < toleranceItemDtoList.size(); i++) {
            int n = 0;
            String code = toleranceItemDtoList.get(i).getCode() == null ? "" : toleranceItemDtoList.get(i).getCode();
            String componentName = toleranceItemDtoList.get(i).getComponentName() == null ? "" : toleranceItemDtoList.get(i).getComponentName();
            String loopType = toleranceItemDtoList.get(i).getLoopType() == null ? "" : toleranceItemDtoList.get(i).getLoopType();
            String nominalSize = toleranceItemDtoList.get(i).getNominalSize() == null ? "" : toleranceItemDtoList.get(i).getNominalSize();
            String coefficient = toleranceItemDtoList.get(i).getCoefficient() == null ? "" : toleranceItemDtoList.get(i).getCoefficient();
            String udl = toleranceItemDtoList.get(i).getUdl() == null ? "" : toleranceItemDtoList.get(i).getUdl();
            String ldl = toleranceItemDtoList.get(i).getLdl() == null ? "" : toleranceItemDtoList.get(i).getLdl();
            String mean = toleranceItemDtoList.get(i).getMean() == null ? "" :toleranceItemDtoList.get(i).getMean();
            String unilateralTolerance = toleranceItemDtoList.get(i).getUnilateralTolerance() == null ? "" : toleranceItemDtoList.get(i).getUnilateralTolerance();
            String sigma = toleranceItemDtoList.get(i).getSigma() == null ? "" :toleranceItemDtoList.get(i).getSigma();

            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, code, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, componentName, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, loopType, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, coefficient, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, nominalSize, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, unilateralTolerance, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, udl, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, ldl, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, mean, ExCellType.TEXT, setFontStyle()));
            exCellList.add(ExUtil.fillToCell(new Integer[]{headIndex[0] + i + 1, headIndex[1] + n++}, sigma, ExCellType.TEXT, setFontStyle()));
        }

        currentRow = currentRow + toleranceItemDtoList.size() + 3;
//        for (int i = 0; i < lastItem.length; i++) {
//            exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, 1 + i}, lastItem[i], ExCellType.TEXT, setFontStyle()));
//        }
//        currentRow++;
//        exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, 6}, summary_basic_size, ExCellType.TEXT, setFontStyle()));
//        exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, 8}, formatDouble(summary_nominalSize, digNum), ExCellType.TEXT, setFontStyle()));
//        exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, 9}, formatDouble(summary_unilateralTolerance, digNum), ExCellType.TEXT, setFontStyle()));

        return exCellList;
    }

    public List<ExCell> buildResult(ToleranceParamDto toleranceParamDto, ToleranceResultDto toleranceResultDto) {
        List<ExCell> exCellList = Lists.newArrayList();
        String[] titleLabels_two = ExportLabelConstant.EXPORT_TOL_RESULT_LABELS;
        currentRow++;
        for (int i = 0; i < titleLabels_two.length; i++) {
            exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, headIndex[0] + 2 * i}, titleLabels_two[i], ExCellType.TEXT, fillTitleStyle()));
        }
        exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, headIndex[0] + 1}, formatDouble(toleranceParamDto.getUsl(), digNum), ExCellType.TEXT, fillResultStyle()));
        exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, headIndex[0] + 3}, formatDouble(toleranceParamDto.getLsl(), digNum), ExCellType.TEXT, fillResultStyle()));
        exCellList.add(ExUtil.fillToCell(new Integer[]{currentRow, headIndex[0] + 5}, formatDouble(toleranceResultDto.getResult() * 100, digNum) + "%", ExCellType.TEXT, fillResultStyle()));
        currentRow = currentRow + 4;
        exCellList.add(ExUtil.fillToCell(new Integer[]{0, 0, 1023, 255, headIndex[0], currentRow, headIndex[0] + 8, currentRow + 44}, toleranceParamDto.getImgPath(), ExCellType.IMAGE));

        return exCellList;

    }

    private XSSFCellStyle fillTitleStyle() {
        XSSFCellStyle style = this.getCurrentWorkbook().createCellStyle();
        XSSFFont font = this.getCurrentWorkbook().createFont();
        style.setFont(font);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

        return style;
    }


    private XSSFCellStyle setFontStyle() {
        XSSFCellStyle style = this.getCurrentWorkbook().createCellStyle();
        XSSFFont font = this.getCurrentWorkbook().createFont();
        style.setFont(font);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

        return style;
    }

    private XSSFCellStyle fillResultStyle() {
        XSSFCellStyle style = this.getCurrentWorkbook().createCellStyle();
        XSSFFont font = this.getCurrentWorkbook().createFont();
        style.setFont(font);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        style.setFillForegroundColor(IndexedColors.YELLOW.index);

        return style;
    }

    private String formatDouble(double str, int dig) {
        return String.format("%." + dig + "f", str);
    }

    @Override
    public XSSFWorkbook getCurrentWorkbook() {
        return workbook;
    }

    @Override
    public List<ExSheet> getSheets() {
        return sheets;
    }

}
